package edu.fim.presentation.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

import edu.fim.presentation.controller.ConsumerController;
import edu.fim.presentation.model.bean.Food;
import edu.fim.presentation.view.model.RankTableModel;

public class RankForm extends AbstractForm {

	private static final long serialVersionUID = -2931565928218195317L;

	private ConsumerController consumerController;

	public RankForm(String title, ConsumerController consumerController) {
		super(title);
		this.consumerController = consumerController;
		initialize();
		update();
	}

	public void update() {
		pack();
	}

	protected void initializeDynamicComponents() {
		
	}

	protected void buildLayout() {
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		JTable rankTable = new JTable(createTableModel());
		JScrollPane scrollPane = new JScrollPane(rankTable);
		getContentPane().add(scrollPane);
		getContentPane().add(createButtonContainer());
	}
	
	private TableModel createTableModel() {
		List<Food> preferences = new ArrayList<>(); 
		preferences = consumerController.getCurrentConsumer().getPreferences();
		String[] columnNames = new String[] { "Food", "Times Consumed" };
		return new RankTableModel(columnNames, preferences);
	}

	private Container createButtonContainer() {
		Container container = new Container();
		container.setLayout(new FlowLayout());
		container.add(createResetComponent());
		container.add(createBackComponent());
		return container;
	}
	
	private Component createResetComponent() {
		AbstractButton button = new JButton("Reset Rank");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				resetRankComponentActionPerformed();
			}
		});
		return button;
	}
	
	private Component createBackComponent() {
		AbstractButton button = new JButton("Back");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				backComponentActionPerformed();
			}
		});
		return button;
	}
	
	private void resetRankComponentActionPerformed() {
		// TODO
	}
	
	private void backComponentActionPerformed() {
		this.setVisible(false);
	}
}